package cn.iocoder.yudao.module.wine.controller.app.supplyinfo;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import cn.iocoder.yudao.module.wine.controller.admin.supplyinfo.vo.SupplyInfoRespVO;
import cn.iocoder.yudao.module.wine.controller.admin.supplyinfo.vo.SupplyInfoSaveReqVO;
import cn.iocoder.yudao.module.wine.controller.app.supplyinfo.vo.AppCreateOrderReqVO;
import cn.iocoder.yudao.module.wine.controller.app.supplyinfo.vo.AppMarketReqVO;
import cn.iocoder.yudao.module.wine.controller.app.supplyinfo.vo.AppSupplyInfoListReqVO;
import cn.iocoder.yudao.module.wine.controller.app.supplyinfo.vo.AppSupplyInfoSaveReqVO;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplylabel.SupplyLabelDO;
import cn.iocoder.yudao.module.wine.service.supplyinfo.SupplyInfoService;
import cn.iocoder.yudao.module.wine.service.supplylabel.SupplyLabelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * @author zyc
 * @createDate 2024/4/22 16:53
 */
@Tag(name = "用户 APP - 供求信息")
@RestController
@RequestMapping("/wine/supply-info")
@Validated
public class AppSupplyInfoController {

    @Resource
    private SupplyLabelService supplyLabelService;
    @Resource
    private SupplyInfoService supplyInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建供求信息")
    public CommonResult<Integer> createSupplyInfo(@Valid @RequestBody AppSupplyInfoSaveReqVO createReqVO) {
        return success(supplyInfoService.appCreateSupplyInfo(createReqVO));
    }

    @GetMapping("/label-list")
    @Operation(summary = "获得特殊标签列表")
    public CommonResult<List<SupplyLabelDO>> getSupplyLabelList() {
        List<SupplyLabelDO> list = supplyLabelService.selectLabelList();
        return success(list);
    }
    @PutMapping("/update")
    @Operation(summary = "更新供求信息")
    public CommonResult<Boolean> updateSupplyInfo(@Valid @RequestBody SupplyInfoSaveReqVO updateReqVO) {
        supplyInfoService.appUpdateSupplyInfo(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "更新供求信息")
    public CommonResult<Boolean> updateSupplyInfoStatus(@Valid @RequestBody SupplyInfoSaveReqVO updateReqVO) {
        supplyInfoService.updateSupplyInfoStatus(updateReqVO);
        return success(true);
    }
    @DeleteMapping("/delete")
    @Operation(summary = "下架供求信息")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteSupplyInfo(@RequestParam("id") Long id) {
        supplyInfoService.appDeleteSupplyOrder(id);
        return success(true);
    }

    @GetMapping("/list")
    @Operation(summary = "供求信息列表")
    public CommonResult<PageResult<SupplyInfoRespVO>> getSupplyInfoList(@Valid AppSupplyInfoListReqVO reqVO) {
        PageResult<SupplyInfoRespVO> page = supplyInfoService.selectList(reqVO);
        List<SupplyInfoRespVO> list = page.getList();
        List<SupplyInfoRespVO> collect = list.stream().peek(item -> {
            String format = AreaUtils.format(item.getAreaId(), " ");
            item.setAreaName(format);
        }).collect(Collectors.toList());
        return success(page.setList(collect));
    }

    @GetMapping("/home-page-list")
    @Operation(summary = "首页供求信息列表")
    public CommonResult<List<SupplyInfoRespVO>> getHomePageSupplyInfoList(@Valid AppSupplyInfoListReqVO reqVO) {
        List<SupplyInfoRespVO> list = supplyInfoService.getHomePageSupplyInfoList(reqVO);
        return success(list.stream().peek(item -> {
            String format = AreaUtils.format(item.getAreaId()," ");
            item.setAreaName(format);
        }).collect(Collectors.toList()));
    }

    @GetMapping("/market-list")
    @Operation(summary = "行情供求信息列表")
    public CommonResult<List<SupplyInfoRespVO>> getMarketSupplyInfoList(@Valid AppMarketReqVO reqVO) {
        List<SupplyInfoRespVO> list = supplyInfoService.getMarketSupplyInfoList(reqVO);
        return success(list.stream().peek(item -> {
            String format = AreaUtils.format(item.getAreaId()," ");
            item.setAreaName(format);
        }).collect(Collectors.toList()));
    }

    @GetMapping("/info-list")
    @Operation(summary = "供求信息详情")
    @PermitAll
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<List<SupplyInfoRespVO>> getSupplyInfoDataList(@RequestParam("ids") Long[] ids) {
        List<SupplyInfoRespVO> data = supplyInfoService.appGetSupplyInfoDataList(ids);
        return success(data);
    }

    @GetMapping("/info")
    @Operation(summary = "供求信息详情")
    @PermitAll
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<SupplyInfoRespVO> getSupplyInfoData(@RequestParam("id") Long id) {
        SupplyInfoRespVO data = supplyInfoService.appGetSupplyInfoData(id);
        data.setAreaName(AreaUtils.format(data.getAreaId(), " "));
        return success(data);
    }

    @GetMapping("/my-supply-info-list")
    @Operation(summary = "我发布的供求信息")
    public CommonResult<List<SupplyInfoRespVO>> getMySupplyInfoList(@Valid AppSupplyInfoListReqVO reqVO) {
        List<SupplyInfoRespVO> list = supplyInfoService.selectMySupplyInfoList(reqVO);
        return success(list.stream().peek(item -> {
            String format = AreaUtils.format(item.getAreaId()," ");
            item.setAreaName(format);
        }).collect(Collectors.toList()));
    }

    @GetMapping("/user-supply-info-list")
    @Operation(summary = "用户发布的供求信息")
    public CommonResult<List<SupplyInfoRespVO>> getUserSupplyInfoList(@Valid AppSupplyInfoListReqVO reqVO) {
        List<SupplyInfoRespVO> list = supplyInfoService.getUserSupplyInfoList(reqVO);
        return success(list.stream().peek(item -> {
            String format = AreaUtils.format(item.getAreaId()," ");
            item.setAreaName(format);
        }).collect(Collectors.toList()));
    }

    @GetMapping("/user-supply-share-list")
    @Operation(summary = "分享用户发布的供求信息")
    public CommonResult<List<SupplyInfoRespVO>> getUserSupplyShareList(@Valid AppSupplyInfoListReqVO reqVO) {
        List<SupplyInfoRespVO> list = supplyInfoService.getUserSupplyShareList(reqVO);
        return success(list.stream().peek(item -> {
            String format = AreaUtils.format(item.getAreaId()," ");
            item.setAreaName(format);
        }).collect(Collectors.toList()));
    }
    /**
     * 生成供求信息二维码
     * @param supplyInfoId
     * @param userId
     * @return
     */
    @GetMapping("/generate-qr-code")
    @Operation(summary = "生成供求信息二维码")
    public CommonResult<byte[]> getCode(@RequestParam("id") Long supplyInfoId,
                                        @RequestParam("sId") Long userId) {
        byte[] bytes = supplyInfoService.generateCode(supplyInfoId,userId);
        return success(bytes);
    }

    /**
     * 生成海报二维码
     * @param userId
     * @return
     */
    @GetMapping("/poster_generate-qr-code")
    @Operation(summary = "生成海报二维码")
    public CommonResult<byte[]> getPosterCode(@RequestParam("id") Long userId) {
        byte[] bytes = supplyInfoService.getPosterCode(userId);
        return success(bytes);
    }


    /**
     * 1.生成担保、担责担保订单
     * @param reqVO
     * @return
     */
    @PostMapping("/guarantee")
    @Operation(summary = "担保供求信息,并生成担保订单")
    public CommonResult<Boolean> guaranteeSupplyInfo(@Valid @RequestBody AppCreateOrderReqVO reqVO) {
        return success(supplyInfoService.guaranteeSupplyInfo(reqVO));
    }


}
